<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class General_model extends CI_Model {

    function login($username, $password) {


        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $this->db->where('user_status', 1);
        $this->db->limit(1);
        $query = $this->db->get('users');

        if ($query->num_rows() == 1) {

            return $query->result();
        } else {
            return false;
        }
    }

    function login_admin($table, $username, $password, $site_id) {
        $this->db->select('username,password,site_id');
        $this->db->from($table);
        $this->db->where('username', $username);
        $this->db->where('password', ($password));
        $this->db->where('site_id', $site_id);
        // $this->db->where('status', 0);
        $this->db->limit(1);
        $query = $this->db->get();

        if ($query->num_rows() == 1) {

            return $query->result();
        } else {
            return false;
        }
    }

    function register($table, $data) {
        $this->db->insert($table, $data);
    }

    function get($table, $start = false, $finish = false, $order = false, $group_by = false, $action = false, $where = false, $join = false, $onjoine = false, $type_join = false) {

        if ($order != false) {
            $this->db->order_by($order);
        }
        if ($group_by != false) {
            $this->db->group_by($group_by);
        }
        if ($where != false) {
            $this->db->where($where);
        }
        if ($join != false) {
            $this->db->join($join, $onjoine, $type_join);
        }
        // var_dump($finish);die();
        if ($finish != false) {
            $this->db->limit($finish, $start);
        }


        $row = $this->db->get($table)->result_array();
        if ($action == false) {
            return $row;
        } else {
            return count($row);
        }
    }

    function get_all($table, $where, $join = false,$order = false) {/// เอาข้อมูลออกมาแถวเดียว
        $this->db->where($where); /// Ex .. id = 2;
        if ($join != false) {
            $this->db->join($join);
        }
        if ($order != false) {
            $this->db->order_by($order);
        }
        $row = $this->db->get($table)->result_array();
        if ($row != false) {
            return $row;
        } else {
            return false;
        }
    }

    function get_row($table, $where, $join = false) {/// เอาข้อมูลออกมาแถวเดียว
        $this->db->where($where); /// Ex .. id = 2;
        if ($join != false) {
            $this->db->join($join);
        }
        $row = $this->db->get($table)->result_array();
        if ($row != false) {
            return $row[0];
        } else {
            return false;
        }
    }

    function get_row_select($select, $table, $where, $join = false) {/// เอาข้อมูลออกมาแถวเดียว
        $this->db->select($select, false);
        $this->db->where($where); /// Ex .. id = 2;
        if ($join != false) {
            $this->db->join($join);
        }
        $row = $this->db->get($table)->result_array();
        if ($row != false) {
            return $row[0];
        } else {
            return false;
        }
    }

    function insert($table, $data) {
        $this->db->insert($table, $data);
    }

    function update($table, $data, $where) {
        $this->db->where($where); /// Ex .. id = 2;
        $this->db->set($data);
        $this->db->update($table);
    }

    function del($table, $where) {
        $this->db->where($where);
        $this->db->delete($table);
    }

    function get_field($select, $table, $where = false,$order = false) {
        if ($where != false) {
            $this->db->where($where);
        }
        if ($order != false) {
            $this->db->order_by($order);
        }
        $this->db->select($select);
        return $this->db->get($table)->result_array();
    }

    function QueryAll($sql, $count = false) {
        $query = $this->db->query($sql);
        if ($count == false) {
            return $query->result_array();
        } else {
            return $this->db->count_all_results();
        }
    }

    function Query1($sql, $count = false) {
        $query = $this->db->query($sql);
        if ($count == false) {
            return $query->result_array();
        } else {
            return $this->db->count_all_results();
        }
    }

}
